<?php

class Damages_Methods_Data_CarDamage_New extends Damages_Methods_Data_CarDamage {

  public function __construct($obj) {
    if ($obj->process)
      $this->process = new Damages_Methods_Data_Processes_ProcessType_Simple($obj->process);
    if ($obj->carData)
      $this->carData = new Damages_Methods_Data_CarData_Simple($obj->carData);
    if ($obj->status)
      $this->status = new Damages_Methods_Data_Dictionary_Status_Simple($obj->status);
    if ($obj->perpetrator)
      $this->perpetrator = new Damages_Methods_Data_Dictionary_Perpetrator_Simple($obj->perpetrator);
    $this->relatedCarDamageId = $obj->relatedCarDamageId;
    if ($obj->ls)
      $this->ls = new Damages_Methods_Data_User_Simple($obj->ls);
    if ($obj->ol)
      $this->ol = new Damages_Methods_Data_User_Simple($obj->ol);
    $this->damageDate = $obj->damageDate;
    $this->dealerReportDate = $obj->dealerReportDate;
    if ($obj->damages)
      foreach ($obj->damages->toArray() as $damage) {
        $this->damages[] = new Damages_Methods_Data_Damage_Simple($damage);
      }
    $this->damageDescription = $obj->damageDescription;
    $this->description = $obj->description;
    if ($obj->estimatedBudgets)
      foreach ($obj->estimatedBudgets->toArray() as $estimatedBudget) {
        $this->estimatedBudgets[] = new Damages_Methods_Data_EstimatedBudget_New($estimatedBudget);
      }
    if ($obj->attachments)
      $this->attachments = new Damages_Methods_Data_Attachments_New($obj->attachments); 
  }

  public function validate() {
    if (!$this->process)
      return false;
    if (!$this->process->id)
      return false;
    if (!$this->carData)
      return false;
    if (!$this->carData->id)
      return false;
    if (!$this->status)
      return false;
    if (!$this->status->id)
      return false;
    if (!$this->perpetrator)
      return false;
    if (!$this->perpetrator->id)
      return false;
    if (!$this->damageDate)
      return false;
    if (!$this->damages)
      return false;
    $processDef = DamagesProcessTypesQuery::create()->findOneById($this->process->id);
    if (!($this->estimatedBudgets || $processDef->getCode() == DamagesProcessTypes::CODE_WAREHOUSE))
      return false;
    if ($processDef->getCode() != DamagesProcessTypes::CODE_WAREHOUSE && !$this->attachments)
      return false;
    if ($processDef->getCode() != DamagesProcessTypes::CODE_WAREHOUSE && !($this->attachments->photos && ($this->attachments->protocol || $processDef->getCode() == DamagesProcessTypes::CODE_DOMESTIC) && $this->attachments->waybill))
      return false;
    return true;
  }

}